package Dungeoneering.Utils;

import org.powerbot.game.api.util.Random;
import org.powerbot.game.api.util.Timer;

/**
 * This is just a collection of commonly used random utilities
 * @author Nicolaas
 *
 */
public class Utilities {
	
	private final static long THINKING_MS = 40;
	
	/**
	 * Will return the time in a formatted time
	 * @param timeMillis the milliseconds we need to format
	 * @return the time in a formatted String
	 */
	public static String getFormattedTime(final long timeMillis) {
		long millis = timeMillis;
		final long seconds2 = millis / 1000;
		final long hours = millis / (1000 * 60 * 60);
		millis -= hours * 1000 * 60 * 60;
		final long minutes = millis / (1000 * 60);
		millis -= minutes * 1000 * 60;
		final long seconds = millis / 1000;
		String hoursString = "";
		String minutesString = "";
		String secondsString = seconds + "";
		String type = "seconds";
		if (minutes > 0) {
			minutesString = minutes + ":";
			type = "minutes";
		} else if (hours > 0 && seconds2 > 0) {
			minutesString = "0:";
		}
		if (hours > 0) {
			hoursString = hours + ":";
			type = "hours";
		}
		if (minutes < 10 && !type.equals("seconds")) {
			minutesString = "0" + minutesString;
		}
		if (hours < 10 && type.equals("hours")) {
			hoursString = "0" + hoursString;
		}
		if (seconds < 10 && !type.equals("seconds")) {
			secondsString = "0" + secondsString;
		}

		if (timeMillis == 1000) {
			type = "second";
		} else if (timeMillis == 60000) {
			type = "minute";
		} else if (timeMillis == 3600000) {
			type = "hour";
		}

		return hoursString + minutesString + secondsString + " " + type;
	}
	
	/**
	 * will sleep for millis amount of milliseconds
	 * @param millis the amount of milliseconds to sleep for
	 */
	public static void sleep(final long millis) {
		Timer timer = new Timer(millis);
        while (timer.isRunning()) {
        }
	}
	
	public static void randSleep(final long millis) {
		Timer timer = new Timer((long)(millis * Random.nextDouble(0.8,1.2)));
        while (timer.isRunning()) {
        }
	}
	
	public static void thinkSleep() {
		Timer timer = new Timer((long)(THINKING_MS * Random.nextDouble(0.5,1.5)));
        while (timer.isRunning()) {
        }
	}

}
